﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StuMngVan.Entities;
using StuMngVan.Dals;

namespace StuMngVan
{
    public partial class FormScore : Form
    {
        public FormScore()
        {
            InitializeComponent();
        }

        private void FormScore_Load(object sender, EventArgs e)
        {
            //DataTable dtCourses = Dal4Score.GetCourses();
            //cboCourse.DataSource = dtCourses;
            //cboCourse.DisplayMember = "CourseName";
            //cboCourse.ValueMember = "CourseID";

            ///【闻祖东 2013-4-2-225853】封装之后，即可将以上四句代码合并为一句，而实际上做的事情并没有任何区别。
            ///尤其是你的项目中多次运用一个控件（ComboBox）绑定值的时候，如果每次都去写那四句代码，那么最终产生的代码行数是
            ///相当可观的。
            CommonUtil.BindData4ComboBox(cboCourse, Dal4Score.GetCourses(), "CourseName", "CourseID");
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            gvScores.DataSource = Dal4Score.SearchAllScores(txtName.Text);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            using (FormAddScore formAddScore = new FormAddScore())
                formAddScore.ShowDialog();

            btnSearch.PerformClick();
        }

        public void btnDelete_Click(object sender, EventArgs e)
        {
            Score scoreSelected = gvScores.SelectedRows[0].DataBoundItem as Score;
            Dal4Score.DeleteScore(scoreSelected);
            MessageBox.Show(string.Format("删除学生【{0}】的课程【{1}】成绩成功！", scoreSelected.Student, scoreSelected.Course));
            
            btnSearch.PerformClick();
        }

        private void btnRanking_Click(object sender, EventArgs e)
        {
            gvScores.DataSource = Dal4Score.SearchAllScoreRanking(Convert.ToString(cboCourse.Text));
        }
    }
}
